package interpreter

import "errors"

type tMockDatabase struct {
	tables map[string]IDataTable
}

func newMockDatabase() IDatabase {
	return &tMockDatabase{
		tables: make(map[string]IDataTable, 0),
	}
}

func (me *tMockDatabase) Register(table IDataTable) {
	me.tables[table.Name()] = table
}

func (me *tMockDatabase) Query(sql string) (error, []IDataRow) {
	parser := newSQLParser()
	e, result := parser.Parse(sql)
	if e != nil {
		return e, nil
	}

	table, ok := me.tables[result.Table]
	if !ok {
		return errors.New("table not found"), nil
	}

	return nil, table.Filter(result.RowFilter)
}

var MockDatabase = newMockDatabase()